// app.js
App({
  d: {
    hostUrl: 'https://app.gemmy.so',
    hostImg: 'http://img.ynjmzb.net',
    hostVideo: 'http://zhubaotong-file.oss-cn-beijing.aliyuncs.com',
    // ceshiUrl:'http://localhost/xunwei/index.php',
    ceshiUrl: 'http://132.232.106.147/xunwei/index.php',
    // ceshiUrl: 'http://192.168.52.131/xunwei/index.php',
    // ceshiUrl: 'https://gzleren.com/xunwei/index.php',
    islogin: false
  },
  onLaunch: function () {
    //调用API从本地缓存中获取数据
    var that=this
    var logs = wx.getStorageSync('logs') || []
    logs.unshift(Date.now())
    wx.setStorageSync('logs', logs);
    //login
   
    that.globalData.islogin = false;
    wx.login({
      success: res => {
        console.log(res.code);
        // 发送 res.code 到后台换取 openId, sessionKey, unionId
       
      }
    });
    
    that.getUserInfo();
  
  },
  getUserInfo:function(cb){
    var that=this
    wx.login({
      success: function (res) {
        var code = res.code;
        console.log("getUserInfo")
        console.log(res)
        wx.getSetting({
          success: res => {
            if (res.authSetting['scope.userInfo']) {
              wx.getUserInfo({
                success: function (res) {
                  console.log(res)
                  that.globalData.userInfo = res.userInfo;
                  typeof cb == "function" && cb(that.globalData.userInfo);
                  that.getUserSessionKey(code);
                }
              })
            } else {
              wx.reLaunch({
                url: '/pages/index/authorize',
              })
            }
          }
        })
      }
    });
    // var that = this
    // that.setData({
    //   islogin: false
    // });
    // if(this.globalData.userInfo){
    //   console.log('success');
    //   typeof cb == "function" && cb(this.globalData.userInfo)
    // }else{
    //   //调用登录接口
      
    //   wx.login({
    //     success: function (res) {
    //       var code = res.code;
    //       wx.getUserInfo({
    //         success: function (res) {
    //           that.globalData.userInfo = res.userInfo;
    //           console.log('res.userInfo');
    //           console.log(res);
               
    //           console.log('res.userInfo');
    //           typeof cb == "function" && cb(that.globalData.userInfo);
    //           //get user sessionKey
    //           //get sessionKey
    //           that.getUserSessionKey(code);
    //         }
    //       });
    //     }
    //   });
    // }
  },

  getUserSessionKey:function(code){
    var that = this;
    wx.request({
      url: that.d.ceshiUrl + '/Api/Login/getsessionkey',
      method:'post',
      data: {
        code: code
      },
      header: {
        'Content-Type':  'application/x-www-form-urlencoded'
      },
      success: function (res) {
        // console.log('getUserSessionKey');
        // console.log(res);
        // console.log('code' + code);
        //--init data        
        var data = res.data;
        console.log('data' + data.openid)
        if(data.status==0){
          wx.showToast({
            title: data.err,
            duration: 2000
          });
          return false;
        }
        that.globalData.userInfo['sessionId'] = data.session_key;
        that.globalData.userInfo['openid'] = data.openid;
        that.onLoginUser();
      },
      fail:function(e){
        wx.showToast({
          title: '网络异常！73',
          duration: 2000
        });
      },
    });
  },
  onLoginUser:function(){
    var that = this;
    var user = that.globalData.userInfo;
    console.log('openid' );
    console.log( user.openid);
    wx.request({
      url: that.d.ceshiUrl + '/Api/Login/authlogin',
      method:'post',
      data: {
        SessionId: user.sessionId,
        gender:user.gender,
        NickName: user.nickName,
        HeadUrl: user.avatarUrl,
        openid: user.openid,
        
      },
      header: {
        'Content-Type':  'application/x-www-form-urlencoded'
      },
      success: function (res) {
        //--init data        
        var data = res.data.arr;
        var status = res.data.status;
        console.log('data');
        console.log(res);
        if(status!=1){
          wx.showToast({
            title: res.data.err,
            duration: 3000
          });
          return false;
        }
        that.globalData.userInfo['id'] = data.ID;
        that.globalData.userInfo['NickName'] = data.NickName;
        that.globalData.userInfo['HeadUrl'] = data.HeadUrl;
        that.d.userId = data.ID;
        that.d.islogin=true
        
        if(!that.d.userId){
          wx.showToast({
            title: '登录失败！',
            duration: 3000
          });
          return false;
        }
      },
      fail:function(e){
        wx.showToast({
          title: '网络异常！119',
          duration: 2000
        });
      },
    });
  },
 globalData:{
    userInfo:null,
    
  },
  onPullDownRefresh: function (){
    wx.stopPullDownRefresh();
  }
});





